package pingan.com.imandtwo.fragments.addressbookfragment.tongxunl;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;

import java.util.List;

import pingan.com.imandtwo.R;

/**
 * Created by 迷人的脚毛！！ on 2018/2/7.
 */

public class MyAdapter extends BaseAdapter {

    private Context mContext;
    private List<ContactVo> mData;

    public MyAdapter(Context mContext, List<ContactVo> mData) {
        super();
        this.mContext = mContext;
        this.mData = mData;
    }

    //定义一个接口
    public interface OnRecycleViewListener{
        void onItemClick(int postion);//单击事件
    }

    //封装私有变量
    private OnRecycleViewListener onRecycleViewListener;

    public void setOnRecycleViewListener(OnRecycleViewListener onItemClickListen){
        this.onRecycleViewListener = onItemClickListen;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return mData.get(position).getId();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder mHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.contart, null);
            mHolder = new ViewHolder();
            mHolder.name = (TextView) convertView.findViewById(R.id.name);
            mHolder.moble = (TextView) convertView.findViewById(R.id.moble);
            mHolder.imgV = (ImageView) convertView.findViewById(R.id.image);
            mHolder.showMsg = (Button) convertView.findViewById(R.id.showMsg);

            convertView.setTag(mHolder);

        } else {
            mHolder = (ViewHolder) convertView.getTag();
            mHolder.imgV.setImageResource(R.mipmap.ic_launcher);
        }

        ContactVo cv = mData.get(position);
        mHolder.name.setText(cv.getName());
        mHolder.moble.setText(cv.getMoble());
        mHolder.imgV.setImageBitmap(cv.getImg());

        if(onRecycleViewListener!=null) {//设置回调
            mHolder.showMsg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onRecycleViewListener.onItemClick(position);
                }
            });
        };

        return convertView;
    }

    class ViewHolder {
        TextView name, moble;
        ImageView imgV;
        Button showMsg;
    }

}

